import { login, getInfo } from "@/api/user"
import { getToken, removeToken, setToken } from "@/utils/auth"

const getDefaultState = () => {
  return {
    token: getToken(),
    name: "",
    avatar: "",
    roles: [],
    roleCode: ""
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  }
  // SET_CODE: (state, code) => {
  //   state.roleCode = localStorage.getItem("token")
  // }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { phone, pwd } = userInfo
    return new Promise((resolve, reject) => {
      // debugger
      login({ phone, pwd, systemCode: sessionStorage.getItem("systemCode") }).then((response) => {
          const { object } = response
          console.log(object.user, "个人信息")
          if (object.user.isFirst == "Y") {
            commit("SET_ROLES", [object.user.roleCode])
            commit("SET_NAME", object.user.userName)
            // window.sessionStorage.setItem("roleCode", object.user.roleCode)
            setToken(object.token)
            sessionStorage.setItem("exp", object.exp)
            // sessionStorage.setItem("token", object.token)
            sessionStorage.setItem("userName", object.user.userName)
            sessionStorage.setItem("userCode", object.user.userCode)
            sessionStorage.setItem("roleName", object.user.roleName)
            sessionStorage.setItem("roleCode", object.user.roleCode)

            resolve(true)
          } else {
            commit("SET_ROLES", [object.user.roleCode])
            commit("SET_NAME", object.user.userName)
            // window.sessionStorage.setItem("roleCode", object.user.roleCode)
            setToken(object.token)
            sessionStorage.setItem("exp", object.exp)
            // sessionStorage.setItem("token", object.token)
            sessionStorage.setItem("userName", object.user.userName)
            sessionStorage.setItem("userCode", object.user.userCode)
            sessionStorage.setItem("roleName", object.user.roleName)
            sessionStorage.setItem("roleCode", object.user.roleCode)

            resolve(false)
          }
        }).catch((error) => {
          reject(error)
        })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token)
        .then((response) => {
          console.log(response)
          const { object } = response

          if (!data) {
            reject("Verification failed, please Login again.")
          }

          const { roles, name, avatar } = data

          // roles must be a non-empty array
          if (!roles || roles.length <= 0) {
            reject("getInfo: roles must be a non-null array!")
          }

          commit("SET_ROLES", roles)
          commit("SET_NAME", name)
          commit("SET_AVATAR", avatar)
          resolve(data)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      // removeToken() // must remove  token  first
      // resolve()

      // logout(state.token)
      //   .then(() => {
      //     removeToken(); // must remove  token  first
      //     commit('RESET_STATE');
      //     resolve();
      //   })
      //   .catch((error) => {
      //     reject(error);
      //   });
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise((resolve) => {
      removeToken() // must remove  token  first
      commit("RESET_STATE")
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
